package com.hdax.easybuy.controller;


import com.hdax.easybuy.api.UserManagerClient;

import com.hdax.easybuy.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserUiController {
    @Resource
    private UserManagerClient userManagerClient;






    @RequestMapping("/user/query")
    @ResponseBody
    public List<User> query(){
        //model.addAttribute("users",userManagerClient.getAll());
        return userManagerClient.getAll();
    }
    @RequestMapping("/user/login")

    public String userlogin(){
        //model.addAttribute("users",userManagerClient.getAll());
        return "pre/login";
    }
    @RequestMapping("test")
    public String as(HttpServletRequest request,HttpSession session,Model model){
        User user= userManagerClient.getUserByuNameAndPwd("admin","123456");

        request.getSession().setAttribute("d",user);
        session.setAttribute("ad",user);
        model.addAttribute("a",user);
        return "userlist";
    }

    @RequestMapping("/user/getUserByuNameAndPwd/{userName}/{password}")
    @ResponseBody
    public User getUserByuNameAndPwd(@PathVariable("userName") String userName,
                                     @PathVariable("password") String password){
        return userManagerClient.getUserByuNameAndPwd(userName,password);
    }

    @RequestMapping("/Login")
    public String Login(String loginName,
                        String password, HttpSession session, HttpServletRequest request,Model model){
        User user= userManagerClient.getUserByuNameAndPwd(loginName,password);
        //HttpSession session=request.getSession(true);
        System.out.println("dfdf"+user.getUserName());
        //session.setAttribute("userinfo",user);
        if(user!=null&&!user.equals("")){
            session.setAttribute("userinfo",user);

//            request.setAttribute("userinfo",user);
            return "redirect:/productUI/queryAllProduct";
        }else{
            session.setAttribute("ms","用户名或密码错误");
            return "pre/login";
        }

    }


}
